Current Date:
I use the map of spain with the limits of its Autonomous Communities. The shape-file of this Communities can be downloaded from: http://centrodedescargas.cnig.es/CentroDescargas/equipamiento.do?method=mostrarEquipamiento
The Communities are colored following their absolute population, by clicking a communitie, a popup appears with the name and the population.
Map of Spain with its Comunidades Autonomas and the population:
library(leaflet)
library(rgdal)
## Loading required package: sp
## rgdal: version: 1.2-6, (SVN revision 651)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
## Path to GDAL shared files: C:/Users/aro/Documents/R/win-library/3.3/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
## Path to PROJ.4 shared files: C:/Users/aro/Documents/R/win-library/3.3/rgdal/proj
## Linking to sp version: 1.2-4
library(raster)
#Loading the shape file of the comunities
sp_shp <- shapefile("recintos_autonomicas_inspire_peninbal_etrs89.shp")
#The population of the communities in 2016 (from http://www.ine.es/jaxiT3/Tabla.htm?t=2915)
pob <- c(86026, 84519, 315794, 2189534, 640647, 146487, 6466996, 2718525, 1087778, 4959968, 7522596, 2041631,2447519, 582206, 1107220, 1042608, 1308563, 8388107)
#Adding Population to the shapefile
sp_pob <- data.frame(sp_shp$CODNUT2, pob)
colnames(sp_pob) <- c("CODNUT2", "POB")
sp_shp_p <- merge(sp_shp, sp_pob, by_x="CODNUT2", by_y="CODNUT2")
#define color palet for the polygon coloring
pal <- colorNumeric("OrRd", domain = sp_shp_p$POB)
#define the names for each polygon
sp_popup <- sprintf("<b>%s</b><br/>Population: %s", sp_shp_p$NAMEUNIT, sp_shp_p$POB)
#bulid up the leaflet display
leaflet(data = sp_shp_p) %>%
setView(lat=40.4165000, lng= -3.7025600, zoom = 6) %>%
addPolygons(fillColor= ~pal(POB), color="000", weight= 2,
popup=sp_popup, fillOpacity = 0.8, highlight = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.6,
bringToFront = TRUE)) %>%
addTiles() %>%
addLegend("bottomright", pal=pal, values = ~POB,
title = "Legend", opacity = 0.7)